/**
 * jims
 */
package com.jims.master.modules.drug.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jims.master.common.data.StringData;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.drug.entity.DrugClassDict;
import com.jims.master.modules.drug.service.DrugClassDictService;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.UserUtils;
import com.jims.master.modules.utils.DrugClassTreeTable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 药品类别字典表Controller
 * @author CTQ
 * @version 2017-03-01
 */
@Controller
@RequestMapping(value = "${adminPath}/drug/drugClassDict")
public class DrugClassDictController extends BaseController {

	@Autowired
	private DrugClassDictService drugClassDictService;
	
	@ModelAttribute
	public DrugClassDict get(@RequestParam(required=false) String id) {
		DrugClassDict entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = drugClassDictService.get(id);
		}
		if (entity == null){
			entity = new DrugClassDict();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(DrugClassDict drugClassDict, Model model) {
		List<DrugClassDict> list = drugClassDictService.findList(drugClassDict);
		List<DrugClassDict> treeList=new DrugClassTreeTable(list).buildTree();
		if(treeList.size()==0){
			treeList=list;
		}
		model.addAttribute("list", treeList);
		return "modules/drug/drugClassDictIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(DrugClassDict drugClassDict, Model model) {
        if(null!=drugClassDict.getClassCode() && "".equals(drugClassDict.getClassCode())){
            drugClassDict.setClassCode(drugClassDict.getClassCode().substring(drugClassDict.getClassCode().length() - 2, 2));
        }
		model.addAttribute("drugClassDict", drugClassDict);
		return "modules/drug/drugClassDictForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(DrugClassDict drugClassDict, Model model) {
	    StringData data=new StringData();
		int i=drugClassDictService.saveClassDict(drugClassDict);
        if(i>0){
            data.setCode("success");
            data.setData("保存药品类别字典表成功");
        }else{
            data.setCode("error");
            data.setData("保存药品类别字典表失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(DrugClassDict drugClassDict) {
	    StringData data=new StringData();
		int i=0;
	    if (StringUtils.isNotBlank(drugClassDict.getId())){
			int count = drugClassDictService.findByParentId(drugClassDict.getId());
	    	if (count>0){
	    		i = -1;
			}else{
				i=drugClassDictService.delete(drugClassDict);
			}
		}

        if(i>0){
            data.setCode("success");
            data.setData("删除药品类别字典表成功");
        }else if(i<0){
			data.setCode("info");
			data.setData("该药品类别下有子类别，不能删除");
		}else{
            data.setCode("error");
            data.setData("删除药品类别字典表失败");
        }
		return data;
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		DrugClassDict drugClassDict=new DrugClassDict();
		User user= UserUtils.getUser();
		drugClassDict.setOrgId(user.getOrgId());
		List<DrugClassDict> list = drugClassDictService.findList(drugClassDict);
		if (list!=null&&list.size()>0){
			for(DrugClassDict dc : list){
				if ((StringUtils.isBlank(extId) || (extId!=null && !extId.equals(dc.getId()) && dc.getParentIds().indexOf("."+extId+".")==-1))){
					Map<String, Object> map = Maps.newHashMap();
					map.put("id", dc.getId());
					map.put("pId", dc.getParent().getId());
					map.put("pIds", dc.getParentIds());
					map.put("name", dc.getClassName());
					mapList.add(map);
				}
			}
		}
		return mapList;
	}

	/**
	 * @param drugClassDict 传递参数
	 * @return StringData    返回类型
	* @throws
	 * @Title: validUnique
	 * @Desciption: (验证药品代码唯一性)
	 * @author CTQ
	 * @date 2017-03-09
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "validUnique")
	@ResponseBody
	public StringData validUnique(DrugClassDict drugClassDict) {
		StringData data=new StringData();
		int i=drugClassDictService.validUnique(drugClassDict);
		if(i<=0){
			data.setCode("success");
//			data.setData("删除药品类别字典表成功");
		}else{
			data.setCode("error");
			data.setData("该类别代码已经存在，请重新输入");
		}
		return data;
	}


}